<template>
  <div class="content">
    <div class="header">
      <div class="title">{{ title }}</div>
      <div class="header-btn"><slot name="header"></slot></div>
    </div>

    <el-table
      :data="data"
      border
      style="width: 100%"
      @selection-change="handelSelectChange"
      v-bind="childernProps"
    >
      <el-table-column
        v-if="showSelectColumn"
        type="selection"
        align="center"
        width="80"
      ></el-table-column>
      <el-table-column
        v-if="showIndexColumn"
        type="index"
        label="序号"
        align="center"
        width="60"
      ></el-table-column>
      <template v-for="(page, index) in pageDatas" :key="index">
        <el-table-column
          v-bind="page"
          align="center"
          :show-overflow-tooltip="true"
        >
          <template #default="scope">
            <span class="span">
              <slot :name="page.slotName" :row="scope.row">{{
                scope.row[page.prop]
              }}</slot>
            </span>
          </template>
        </el-table-column>
      </template>
    </el-table>

    <div class="footer" v-show="showFooter">
      <slot name="footer">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.currentPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listCount"
        >
        </el-pagination>
      </slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
  emits: ['selectChange', 'update:page'],
  props: {
    title: {
      type: String
    },
    pageDatas: {
      type: Array
    },
    data: {
      type: Array
    },
    showIndexColumn: {
      type: Boolean,
      default: false
    },
    showSelectColumn: {
      type: Boolean,
      default: false
    },
    listCount: {
      type: Number,
      default: 0
    },
    page: {
      type: Object,
      default: () => {
        return { currentPage: 0, pageSize: 10 }
      }
    },
    childernProps: {
      type: Object,
      default: () => {
        return {}
      }
    },
    showFooter: {
      type: Boolean,
      default: true
    }
  },
  setup(props, { emit }) {
    const handelSelectChange = (value: any) => {
      emit('selectChange', value)
    }
    const handleCurrentChange = (currentPage: number) => {
      emit('update:page', { ...props.page, currentPage })
    }
    const handleSizeChange = (pageSize: number) => {
      emit('update:page', { ...props.page, pageSize })
    }
    return {
      handelSelectChange,
      handleCurrentChange,
      handleSizeChange
    }
  }
})
</script>

<style scoped>
.content {
  width: 100%;
  background-color: rgb(255, 255, 255);
}
.header {
  height: 50px;
  line-height: 50px;
  display: flex;
  justify-content: space-between;
}
.table {
  width: 100%;
}
.title {
  padding-left: 20px;
}
.header-btn {
  padding-right: 20px;
}
.footer {
  width: 100%;
  padding: 10px 0 0 0;
}
.el-pagination {
  text-align: right;
}
.span {
  font-size: 12px;
}
</style>
